function Write-Error {
    Param (
        [string]$ErrorLogFolder,
        [System.Management.Automation.ErrorRecord]$ErrorVar
    )

    if (!$ErrorVar) {
        return
    }

    $ErrorInfo = @{
        "Timestamp" = (Get-Date).ToString()
        "InvokeLine" = $ErrorVar.InvocationInfo.Line
        "InvokeLinePos" = $ErrorVar.InvocationInfo.ScriptLineNumber
        "InvokeLineMsg" = $ErrorVar.InvocationInfo.PositionMessage
        "ExceptionType" = ($ErrorVar.Exception | Get-Member | Select-Object -ExpandProperty TypeName -Unique)
        "ExceptionMessage" = $ErrorVar.Exception.Message
    }

    if ($ErrorLogFolder) {
        $ErrorFilePath = Join-Path -Path $ErrorLogFolder -ChildPath 'ErrorLog.txt'
        try {
            $arr = @()
            # Check if ErrorLog.txt exists.
            if (Test-Path -Path $ErrorFilePath) {
                # Read errors from the error log.
                $TxtFileContent = Get-Content -LiteralPath $ErrorFilePath -ErrorAction Stop
                $ErrorsFromFile = $TxtFileContent | ConvertFrom-Json
                
                # Add every error from the file into an array.
                foreach ($err in $ErrorsFromFile) {
                    $arr += $err
                }
            }
            # Add new error to array.
            $arr += $ErrorInfo
            # Write errors in array to ErrorLog.txt
            $arr | ConvertTo-Json | Out-File -FilePath $ErrorFilePath
        }
        catch {
            Write-Host "Failed to write the error to ErrorLog.txt." -ForegroundColor Yellow
            Start-Sleep -Milliseconds 100
        }
    }

    <#
    .SYNOPSIS
        Writes error to a log file.
    .DESCRIPTION
        Adds error summary to a log file.
    .EXAMPLE
        catch {
            Write-LSError -ErrorVar $_ -ErrorLogFolder (Get-Location)
        }
        Writes error summary into ErrorLog.txt in current location.
    .INPUTS
        None.
    .OUTPUTS
        None.
    #>
}